from storage_mysql import Todolist
from flask import Flask,request

app = Flask(__name__)
a =Todolist()
app.config['JSON_AS_ASCII'] = False
def index():
    return '<h1>Hello,54<h1>'
app.add_url_rule('/',view_func=index)
@app.route('/dotos',methods=['GET'])
def doto_index():
    return {'todos':[x.__dict__ for x in a.list_todos()]}
@app.route('/doto',methods=['POST'])
def create_todo():
    if 'title' not in request.json:
        return '参数错误，必须提供 title 参数', 400
    todo = a.create(request.json['title'])
    return todo.__dict__
@app.route('/doto/mark',methods=['GET'])
def mark():
    if 'id' not in request.args:
        return '参数错误，必须提供 id 参数', 400
    id = int(request.args['id'])
    todo = a.mark_completed(id)
    if todo is not None:
        return todo.__dict__
    else:
        return f'todo not found, id = {id}', 404
@app.route('/doto/delete',methods=['GET'])
def marks():
    if 'id' not in request.args:
        return '参数错误，必须提供 id 参数', 400
    id = int(request.args['id'])
    todo = a.delete(id)
    return todo.__dict__
if __name__ =='__main__':
    app.run()